package com.api.gbhg.zengping.utils;

import weaver.conn.RecordSet;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: gbhg-oa
 * @author: Mr.zeng
 * @create: 2021-01-28 09:49
 **/
public class RecursionUtil {

   final  RecordSet rs=new RecordSet();

    /**
     * 递归方法，得到所有当前单位下所有单位
     * @param list
     * @return
     */
    public  List<Integer> getSubList(List list){
        for (int i = 0; i < list.size(); i++) {
            String sql="select * from hrmsubcompany where supsubcomid ="+list.get(i);
            rs.executeQuery(sql);
            while (rs.next()){
                list.add(rs.getInt("id"));
            }
        }
        return list;
    }
    public Map[] getCurrentDept(List<Integer> list){
        RecordSet rs=new RecordSet();
        Map<String,Object>[] map=null;
        for (int i = 0; i < list.size(); i++) {
            String sql="select * from hrmsubcompany where supsubcomid ="+list.get(i)+" ";
            rs.executeQuery(sql);
            map=new HashMap[rs.getArray().size()];
            int k=0;
            if(rs.getArray().size()==0){
                RecordSet rs1=new RecordSet();
                String sql1="select * from hrmsubcompany where  id="+list.get(i);
                rs1.executeQuery(sql1);
                map=new HashMap[rs1.getArray().size()];
                while (rs1.next()){
                    map[k]=new HashMap<>();
                    map[k].put("key",rs1.getString("id"));
                    map[k].put("value",rs1.getString("id"));
                    map[k].put("label",rs1.getString("subcompanyname"));
                    return map;
                    //递归查询是否有分部
                }
            }else {
               return getSubist1(list);
            }
        }
        //不可能执行到这里
        return null;
    }
    public static Map[] getSubist1(List<Integer> list){
        RecordSet rs=new RecordSet();
        Map<String,Object>[] map=null;
        for (int i = 0; i < list.size(); i++) {
            String sql="select * from hrmsubcompany where supsubcomid ="+list.get(i)+" ";
            rs.executeQuery(sql);
            map=new HashMap[rs.getArray().size()];
            int k=0;
            while (rs.next()){
                map[k]=new HashMap<>();
                map[k].put("key",rs.getString("id"));
                map[k].put("value",rs.getString("id"));
                map[k].put("label",rs.getString("subcompanyname"));
                //递归查询是否有分部
                List<Integer> list1= new ArrayList<>();
                list1.add(rs.getInt("id"));

                Map[] subist1 = getSubist1(list1);
                if(subist1!=null){
                    map[k].put("children",subist1);
                }
                k++;
            }
        }
        return map;
    }

    /**
     * List转成字符串
     * @param list
     * @param separator 分隔
     * @return
     */
    public static String listToString1(List list, char separator) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < list.size(); i++) {
            sb.append(list.get(i)).append(separator);

        }
        return sb.substring(0, sb.toString().length() - 1);
    }

    public static void main(String[] args) {
        List list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        String s = listToString1(list, ',');
        System.out.println(s);
    }

}
